package com.numberone.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.numberone.system.mapper.AddressMapper;
import com.numberone.system.domain.Address;
import com.numberone.system.service.IAddressService;
import com.numberone.common.support.Convert;

/**
 * 收货地址 服务层实现
 * 
 * @author numberone
 * @date 2020-09-27
 */
@Service
public class AddressServiceImpl implements IAddressService 
{
	@Autowired
	private AddressMapper addressMapper;

	/**
     * 查询收货地址信息
     * 
     * @param id 收货地址ID
     * @return 收货地址信息
     */
    @Override
	public Address selectAddressById(Long id)
	{
	    return addressMapper.selectAddressById(id);
	}
	
	/**
     * 查询收货地址列表
     * 
     * @param address 收货地址信息
     * @return 收货地址集合
     */
	@Override
	public List<Address> selectAddressList(Address address)
	{
	    return addressMapper.selectAddressList(address);
	}
	
    /**
     * 新增收货地址
     * 
     * @param address 收货地址信息
     * @return 结果
     */
	@Override
	public int insertAddress(Address address)
	{
		int count = addressMapper.selectCount(address.getUserId());
		if (count == 0) {
			//  若没有地址，把当前增加的地址设为默认地址
			address.setStatus(1);
		} else {
			// 若有地址
			if (address.getStatus() != 0) {
				// 若这个地址需要是默认地址，则将其他的地址设置为非默认状态
				Address defaultAddress = addressMapper.selectDefaultAddress(address.getUserId());
				defaultAddress.setStatus(0);
				addressMapper.updateAddress(defaultAddress);
				address.setStatus(1);
			} else {
				address.setStatus(0);
			}
		}
		return addressMapper.insertAddress(address);
	}
	
	/**
     * 修改收货地址
     * 
     * @param address 收货地址信息
     * @return 结果
     */
	@Override
	public int updateAddress(Address address)
	{
		Address addressById = selectAddressById(address.getId());
		if (address.getStatus() != null)
		{
			//判断是否修改为默认地址
			if (address.getStatus() != 0) {
				addressById.setStatus(1);
				// 若这个地址需要是默认地址，则将其他的地址设置为非默认状态
				Address defaultAddress = addressMapper.selectDefaultAddress(addressById.getUserId());
				System.out.println(defaultAddress.getId()+"=========");
				defaultAddress.setStatus(0);
				addressMapper.updateAddress(defaultAddress);
			}
		}
		return addressMapper.updateAddress(address);
	}

	/**
	 * 删除地址
	 * @param id 收货地址ID
	 * @return
	 */
	@Override
	public int deleteAddressById(Long id) {
		Address address = addressMapper.selectAddressById(id);
		//判断删除地址信息是否为默认地址
		if (address.getStatus() == 1)
		{
			//若是默认地址，需要将其他1个设置为默认地址
			Address addressByUserId = new Address();
			List<Address> addressList = selectAddressList(addressByUserId);
			for(Address i : addressList)
			{
				if (!i.getId().equals(id))
				{
					i.setStatus(1);
					addressMapper.updateAddress(i);
				}
			}
			return addressMapper.deleteAddressById(id);
		}else {
			return addressMapper.deleteAddressById(id);
		}
	}

	/**
     * 删除收货地址对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAddressByIds(String ids)
	{
		return addressMapper.deleteAddressByIds(Convert.toStrArray(ids));
	}
	
}
